package com.winning.sjzc.zygl.controller;

import com.winning.sjzc.common.utils.*;
import com.winning.sjzc.zygl.entity.Zxx;
import com.winning.sjzc.zygl.entity.Zyxx;
import com.winning.sjzc.zygl.service.ZyglService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/zygl")
public class ZyglController {

    @Autowired
    private ZyglService zyglService;

    @GetMapping("/queryZyTree")
    public ResponseEntity queryZyTree() {
        return zyglService.queryZyTree();
    }

    @PostMapping("/saveZy")
    public ResponseEntity saveZy(@RequestBody Zyxx zyxx) {
        if (!Common.validateParams(zyxx.getZydm())) {
            return ResponseEntity.error(201, "参数为空");
        }
        return zyglService.saveZyxx(zyxx);
    }

    @GetMapping("/delZy")
    public ResponseEntity delZy(@RequestParam String zydm) {
        return zyglService.delZyxxByZydm(zydm);
    }

    @GetMapping("/getZy")
    public ResponseEntity getZy(@RequestParam String zydm) {
        return zyglService.getZyxxByZydm(zydm);
    }

    @PostMapping("/updateZy")
    public ResponseEntity updateZy(@RequestBody Zyxx zyxx) {
        if (!Common.validateParams(zyxx.getZydm())) {
            return ResponseEntity.error(201, "参数为空");
        }
        return zyglService.updateZyxx(zyxx);
    }

    @PostMapping("/queryList")
    public ResponseEntity queryZxxByZydm(@RequestBody Map<String, Object> param) {
        PageBean page = (PageBean) ConverterUtils.map2ObjectWithoutNullValue((Map<String, String>) param.get("page"), PageBean.class);
        param.put("page", page);
        List<Zxx> zxxes = zyglService.queryZxxByParam(param);
        return PaginationResponseEntity.ok(page).put("rows", zxxes);
    }

    @PostMapping("/saveZyx")
    public ResponseEntity saveZxx(@RequestBody Zxx zxx) {
        if (!Common.validateParams(zxx.getZydm())) {
            return ResponseEntity.error(201, "参数为空");
        }
        return zyglService.saveZxx(zxx);
    }

    @GetMapping("/delZyx")
    public ResponseEntity delZxx(@RequestParam String zdm, @RequestParam String zydm) {
        if (!Common.validateParams(zdm, zydm)) {
            return ResponseEntity.error(201, "参数为空");
        }
        Zxx zxx = new Zxx();
        zxx.setZdm(zdm);
        zxx.setZydm(zydm);
        return zyglService.delZxx(zxx);
    }

    @GetMapping("/getZyx")
    public ResponseEntity getZxx(@RequestParam String zdm, @RequestParam String zydm) {
        if (!Common.validateParams(zdm, zydm)) {
            return ResponseEntity.error(201, "参数为空");
        }
        Zxx zxx = new Zxx();
        zxx.setZdm(zdm);
        zxx.setZydm(zydm);
        return zyglService.getZxxByZydmAndZdm(zxx);
    }

    @PostMapping("/updateZyx")
    public ResponseEntity updateZxx(@RequestBody Zxx zxx) {
        if (!Common.validateParams(zxx.getZdm(), zxx.getZydm())) {
            return ResponseEntity.error(201, "参数为空");
        }
        return zyglService.updateZxx(zxx);
    }

}
